<?php
/**
 * 部门管理
*/
apf_require_class("Bll_BLL");
apf_require_class('Bll_Hr_Property');
class Bll_Hr_Department extends BLL
{
    /**
     * 单个部门信息
     * @param int $id
     */
    public function find_dep_by_id ($id) {
        return $this->get_dep_dao()->find_by_id($id);
    }

    /**
     * 查询岗位列表时使用
     *
     * @param array $where
     */
    public function find_where ($params)
    {
        if (!empty($params['company_id'])) {
            $_where['company_id'] = intval($params['company_id']);
        }
        if (!empty($params['city_id'])) {
            if(is_numeric($params['city_id'])){
                $_where['city_id'] = intval($params['city_id']);
            }else{
                $_where["city_id IN(".implode(',',$params['city_id']).") AND 1=?"]=1;
            }
        }
        if (!empty($params['bu_id'])) {
            $_where['bu_id'] = intval($params['bu_id']);
        }
        if (!empty($params['function_id'])) {
            $_where['function_id'] = intval($params['function_id']);
        }
        if (!empty($params['is_deleted'])) {
            $_where['is_deleted'] = intval($params['is_deleted']);
        }
        return $this->get_dep_dao()->find_short_assoc($_where, "", 0);
    }

    /**
     *
     * @param array $where
     * @param int $limit
     *            10
     * @param int $offset
     */
    public function get_departmeng_list ($where, $limit, $offset, $pro_list = array())
    {
        $data = array();
        $tmp_data = $this->get_dep_dao()->find($where,
                " city_id ASC,department_id ASC", $limit, $offset);
        if (empty($tmp_data)) {
            return $data;
        }
        $data = $this->build_data($tmp_data, $pro_list);
        return $data;
    }

    /**
     * 重组数据
     *
     * @param array $list
     * @param array $pro_list
     */
    public function build_data ($data, $pro_list = array())
    {
        if (empty($pro_list)) {
            apf_require_class("Bll_Hr_Property");
            $bll = new Bll_Hr_Property();
            $pro_list['company'] = $bll->get_company_list();
            $pro_list['citylist'] = $bll->get_sort_city_list();
            $pro_list['business'] = $bll->get_business_list();
            $pro_list['function'] = $bll->get_function_list();
        }
        $result = array();
        foreach ($data as $k => $val) {
            $val['city_name'] = $pro_list['citylist'][$val['city_id']];
            $val['cost_city_name'] = $pro_list['citylist'][$val['cost_city']];
            $val['bu_name'] = $pro_list['business'][$val['bu_id']]['bu_name'];
            $val['function_name'] = $pro_list['function'][$val['function_id']]['function_name'];
            $result[$val['department_id']] = $val;
        }
        return $result;
    }

    /**
     * 得到统计的数字
     */
    public function get_departmeng_count ($where)
    {
        return $this->get_dep_dao()->find_count($where);
    }

    /**
     * 增加部门
     *
     * @param array $data
     * @return int
     */
    public function add_department ($data = array()){
        $dep_id = 0;
        if (empty($data) || empty($data['function_id']) || empty($data['bu_id'])) {
            return $dep_id;
        }
        $dep_id = $this->get_dep_dao()->insert($data);
        return $dep_id;
    }

    /**
     * 修改部门信息
     *
     * @param int $dep_id
     * @param array $data
     * @param boolean $status为了做累加动作
     *            $data = array('plan_num'=>'plan_num+1')
     * @return boolean unknown
     */
    public function update_department ($dep_id, $data, $status = false)
    {
        $res = false;
        if (empty($dep_id) || empty($data)) {
            return $res;
        }
        $res = $this->get_dep_dao()->update_by_id($dep_id, $data, $status);
        return $res;
    }

    /**
     * 删除部门逻辑删除
     *
     * @param int $dep_id
     * @return boolean
     */
    public function delete_department ($dep_id)
    {
        $status = false;
        if (empty($dep_id)) {
            return $status;
        }
        $have_user = $this->_check_department_users($dep_id);
        if (! $have_user) {
            return $status;
        }
        $data = array(
                'is_deleted' => 2
        );
        $status = $this->get_dep_dao()->update_by_id($dep_id, $data);
        $job_bll = new Bll_Hr_Job();
        $job_bll->delete_job_by_department($dep_id);
        return $status;
    }

    /**
     * 检测部门是否还有员工
     *
     * @param int $dep_id
     * @return boolean
     */
    private function _check_department_users ($dep_id)
    {
        $status = false;
        if (empty($dep_id)) {
            return $status;
        }
        $data = $this->get_dep_dao()->find_by_id($dep_id);
        if (empty($data) || $data['fact_num']) {
            return $status;
        }
        return true;
    }

    /**
     * 检测部门是否存在
     *
     * @param array $data
     * @return boolean
     */
    public function check_same_department ($data)
    {
        $status = true;
        if (empty($data['department_name']) || empty($data['company_id']) ||
                 empty($data['city_id']) || empty($data['function_id']) ||
                 empty($data['bu_id'])) {
            return $status;
        }
        $where = array(
                'company_id' => $data['company_id'],
                'city_id' => $data['city_id'],
                'function_id' => $data['function_id'],
                'bu_id' => $data['bu_id'],
                'department_name' => $data['department_name'],
                'is_deleted' => 1
        );
        $data = $this->get_dep_dao()->find($where);
        if (empty($data)) {
            return false;
        }
        return $data[0]['department_id'];
    }

    /**
     * 根据部门ID查询部门信息
     *
     * @param array $ids
     * @return array
     */
    public function get_department_by_ids ($ids, $is_deleted = 1)
    {
        $return = array();
        if (!empty($ids)) {
            $ids = array_unique($ids);
            $where['department_id IN(' . implode(',', $ids) . ') AND ?'] = 1;
            if($is_deleted==1 || $is_deleted==2){
                $where['is_deleted'] = $is_deleted;
            }
            $return = $this->get_dep_dao()->find_short_assoc($where ,'' ,0);
        }

        return $return;
    }

    /**
     * 根据部门ID查询部门信息
     *
     * @param array $id
     * @return array
     */
    public function get_department_by_id ($id, $is_deleted = 1)
    {
        $return = array();
        if ($id) {
            $where['department_id'] = intval($id);
            $where['is_deleted'] = $is_deleted;
            $return = $this->get_dep_dao()->find_row($where);
        }

        return $return;
    }
    private $citys;//城市
    private $company;//签约公司
    private $business;//事业部
    private $bll_property;
    /**
     * 获取部门详细信息
     * @param unknown_type $id
     * @param unknown_type $is_deleted
     */
    public function get_department_info_by_id($id, $is_deleted = 1){
        $depart = $this->get_department_by_id($id,$is_deleted);
        if(empty($depart)){
            return $depart;
        }
        if(!$this->bll_property){
            $this->bll_property = new Bll_Hr_Property();
        }
        if(!$this->citys){
            $this->citys = $this->bll_property->get_city_list();
        }
        if(!$this->company){
            $this->company = $this->bll_property->get_agree_company_list();
        }
        if(!$this->business){
            $this->business = $this->bll_property->get_business_list();
        }

        $depart['bu_name'] = $this->business[$depart['bu_id']]['bu_name'];
        $depart['city_name'] = $this->citys[$depart['city_id']]['city_name'];
        $depart['company_name'] = $this->company[$depart['company_id']]['company_name'];
        return $depart;
    }
    /**
     *
     * @return Ambigous <Aifang_Core_DAO, NULL>
     */
    private function get_dep_dao ()
    {
        $dao = $this->get_dao('Dao_Hr_Department');
        return $dao;
    }
}